package com.ffws.carInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ffws.base.model.Label;
import com.ffws.base.model.RestResponse;
import com.ffws.carInfo.mapper.FireCarMapper;
import com.ffws.carInfo.mapper.FireCarsTypeMapper;
import com.ffws.carInfo.mapper.FireHydrantMapper;
import com.ffws.carInfo.model.dto.FireCarDto;
import com.ffws.carInfo.model.po.FireCar;
import com.ffws.carInfo.model.po.FireCarsType;
import com.ffws.carInfo.model.po.FireHydrant;
import com.ffws.carInfo.service.CarInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author JC
 * @description TODO
 * @Date 2023-9-3 20:43
 * @Version 1.0
 */
@Service
@Slf4j
public class CarInfoServiceImpl implements CarInfoService {

    @Resource
    FireHydrantMapper fireHydrantMapper;

    @Resource
    FireCarsTypeMapper fireCarsTypeMapper;

    @Resource
    FireCarMapper fireCarMapper;

    @Override
    public RestResponse<List<FireHydrant>> getAllFireHydrant(Long tabId) {
        List<FireHydrant> fireHydrants = fireHydrantMapper.selectList(new LambdaUpdateWrapper<FireHydrant>()
                .eq(FireHydrant::getTabId, tabId));
        return RestResponse.success(fireHydrants);
    }

    @Override
    public RestResponse<List<Label>> getRealTimeStatus(Long tabId) {
        Long allCar = fireCarMapper.selectCount(new LambdaUpdateWrapper<FireCar>()
                .eq(FireCar::getTabId, tabId)
                .ne(FireCar::getStatus, 2)).longValue();
        Long notPresentCar = fireCarMapper.selectCount(new LambdaUpdateWrapper<FireCar>()
                .eq(FireCar::getTabId, tabId)
                .eq(FireCar::getStatus, 0)).longValue();
        Long presentCar = fireCarMapper.selectCount(new LambdaUpdateWrapper<FireCar>()
                .eq(FireCar::getTabId, tabId)
                .eq(FireCar::getStatus, 1)).longValue();
        List<Label> labels = new ArrayList<>();
        labels.add(new Label(allCar,"派出车辆"));
        labels.add(new Label(notPresentCar,"未到达"));
        labels.add(new Label(presentCar,"已到达"));
        return RestResponse.success(labels);
    }

    @Override
    public RestResponse<List<FireCarDto>> getAllFireCars(Long tabId) {
        List<FireCar> fireCars = fireCarMapper.selectList(new LambdaUpdateWrapper<FireCar>()
                .eq(FireCar::getTabId, tabId));
        List<FireCarDto> list = new ArrayList<>();
        fireCars.forEach(fireCar -> {
            FireCarDto fireCarDto = new FireCarDto();
            BeanUtils.copyProperties(fireCar,fireCarDto);
            FireCarsType fireCarsType = fireCarsTypeMapper.selectOne(new LambdaQueryWrapper<FireCarsType>()
                    .eq(FireCarsType::getId, fireCarDto.getTypeId()));
            fireCarDto.setType(fireCarsType.getContent());
            list.add(fireCarDto);
        });
        return RestResponse.success(list);
    }

}
